using System;
using System.Collections.Generic;
using System.Text;

namespace WGT
{
    public partial class Dialog
    {
        public bool DropDown<T>(pos p, Func<IList<T>> list, Func<T, string> key, Func<T, string> value, Action<string> set, string selected, string ctx)
        {
            return DropDown(p, 1, list, key, value, set, selected, ctx);    
        }

        public bool DropDown<T>(pos p, int size, Func<IList<T>> list, Func<T, string> key, Func<T, string> value, Action<string> set, string selected, string ctx)
        {
            int id = 0;
            pos op = null;
            string os = "", octx = null;

            bool rval = false;

            GetPut(ref os, ref selected);
            GetPut(ref octx, ref ctx);
            GetPut(ref op, ref p);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;
                RenderDropDown( selected, p, list, key, value, id, set, size );
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();

            if (mode == MODE.UPDATE)
            {
                if (os != selected)
                {
                    U(@"$('{0}').value={1};", sid, selected);
                }
                if (!p.Equals(op))
                {
                    U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                }
                if (octx != ctx)
                {
                    E(sid);
                    RenderDropDown(selected, p, list, key, value, id, set, size);
                }
            }
            else if (mode == MODE.ERASE)
            {
                E(sid);
            }
            else if (mode == MODE.EVENT)
            {
                if (sender == sid)
                {
                    if (set != null) set(event_args[1]);
                    bUpdate = true;
                    rval = true;
                }
            }

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }

            return rval;
        }

        private void RenderDropDown<T>(string selected, pos p, Func<IList<T>> list, Func<T, string> key, Func<T, string> value, int id, Action<string> set, int size)
        {
            var items = P(list);
                
            var html = new StringBuilder(@"<select id='{0}' Size='{1}' onchange=a(w.event(['{0}',this.value])); >");
            var sel = false;
            foreach (var item in items)
            {
                var k = key(item);
                var v = value(item);
                var b = k == selected;
                sel |= b;
                html.Append("<option value='").Append(k).Append("' ").Append((b).ToSelected())
                    .Append(">").Append(v).Append("</option>");
            }
            html.Append("</select>");
            S(html.ToString(), id.ToString(), size);
            U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
            //UP(id.ToString(), attrs);

            if(!sel && items.Count > 0) set(key(items[0]));
            if (items.Count == 0) set("0");
        }
    }
}